<?php
/*
 * @description: Контроллер для работы с лекциями
 * @copyright Copyright (c) 2012-2017 Marin V. , Tykva A. , Kozel R. , Sokolov D.
 * @version 1.0
 * @link http://lecturebox.ru
*/

class CronController extends Model_Controller
{
    public function init()
    {
        /**
         * В parent::init() выполняется создание объекта Zend_Auth - $this->auth
         * и определение id текущего пользователя - $this->user_id
         */
        parent::init();
    }

    public function indexAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();                
    }
    

    public function dbvideosAction()
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        
        $vid = new Model_Lecture();
        
        $videos = $vid->getAllLectures();
        //print_r($videos);
        
        //print_r($vid);
        foreach ($videos as $item){
            if($item->writecode == 'dub'){
                $changeTime = new Model_Lecture($item->lecture_id);
                $authenticationURL = 'https://www.google.com/accounts/ClientLogin'; 
                try{
                    $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
                            $this->getFrontController()->getParam('bootstrap')->getOption('yusername'),                                                                                                                                                                               
                            $this->getFrontController()->getParam('bootstrap')->getOption('ypassword'),                                                                          
                            $this->getFrontController()->getParam('bootstrap')->getOption('yservice'),                                                                                                                          
                            null,                                                                                                                                 
                            $this->getFrontController()->getParam('bootstrap')->getOption('yname'),                                                                              
                            null,                                                                                                                             
                            null,                                                                                                                           
                            $authenticationURL);

                    $developerKey = $this->getFrontController()->getParam('bootstrap')->getOption('ycode');                                                                                              
                    $httpClient->setHeaders('X-GData-Key', "key=$developerKey"); //ключ разработчика

                    $yt = new Zend_Gdata_YouTube($httpClient);
                    $videoEntryToDelete = $yt->getVideoEntry($item->youtube_id, null, true);
                    $yt->delete($videoEntryToDelete);
                    
                    $ranks = new Model_Rank();
                        $ran = $ranks->getRankByLectureId($item->lecture_id);
                        foreach ($ran as $rank){
                           $rank->delete();
                        }
                        $timeTables = new Model_Timeline();
                        $timeL = $timeTables->getTimelineByLecture($item->lecture_id);
                        foreach ($timeL as $timeTable){
                            $timeTable->delete();
                        }
                    $changeTime->delete();
                    
                } catch (Zend_Gdata_App_Exception $e){
                    
                }
                
            }
            if($item->writecode == 'deleted'){
                $changeTime = new Model_Lecture($item->lecture_id);
                $ranks = new Model_Rank();
                        $ran = $ranks->getRankByLectureId($item->lecture_id);
                        foreach ($ran as $rank){
                           $rank->delete();
                        }
                        $timeTables = new Model_Timeline();
                        $timeL = $timeTables->getTimelineByLecture($item->lecture_id);
                        foreach ($timeL as $timeTable){
                            $timeTable->delete();
                        }
                $changeTime->delete();
            }
            
        }
        
    }
    
    public function seavidAction() 
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        $vid = new Model_Lecture();
        
        $videos = $vid->getAllLectures();
        //print_r($videos);
        //$authenticationURL = 'https://www.google.com/accounts/ClientLogin'; 
                try{
//                    $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
//                            $this->getFrontController()->getParam('bootstrap')->getOption('yusername'),                                                                                                                                                                               
//                            $this->getFrontController()->getParam('bootstrap')->getOption('ypassword'),                                                                          
//                            $this->getFrontController()->getParam('bootstrap')->getOption('yservice'),                                                                                                                          
//                            null,                                                                                                                                 
//                            $this->getFrontController()->getParam('bootstrap')->getOption('yname'),                                                                              
//                            null,                                                                                                                             
//                            null,                                                                                                                           
//                            $authenticationURL);
//
//                    $developerKey = $this->getFrontController()->getParam('bootstrap')->getOption('ycode');                                                                                              
//                    $httpClient->setHeaders('X-GData-Key', "key=$developerKey"); //ключ разработчика

                    //$yt = new Zend_Gdata_YouTube($httpClient);
                   $yt = new Zend_Gdata_YouTube();
                } catch (Zend_Gdata_App_Exception $e){
                
                }
        //print_r($vid);
        foreach ($videos as $item){
            try{
                //if(!empty($yt->getVideoEntry($item->youtube_id)->getVideoState())){
                    if($yt->getVideoEntry($item->youtube_id)->getVideoState() =='Duplicate video'){
                        $videos = new Model_Lecture($item->lecture_id);
                        $videos->writecode = 'dub';
                        $videos->course_id = 0;
                        $videos->save();
                        $ranks = new Model_Rank();
                        $ran = $ranks->getRankByLectureId($item->lecture_id);
                        foreach ($ran as $rank){
                           $rank->delete();
                        }
                        $timeTables = new Model_Timeline();
                        $timeL = $timeTables->getTimelineByLecture($item->lecture_id);
                        foreach ($timeL as $timeTable){
                            $timeTable->delete();
                        }
                    }
                //}
        }  catch (Zend_Gdata_App_Exception $e){
            if(substr($e->getMessage(),36) == 'Video not found' || substr($e->getMessage(),36) == 'Invalid id'){
                $videos = new Model_Lecture($item->lecture_id);
                $videos->writecode = 'deleted';
                $videos->save();
                $ranks = new Model_Rank();
                $ran = $ranks->getRankByLectureId($item->lecture_id);
                foreach ($ran as $rank){
                   $rank->delete();
                }
                $timeTables = new Model_Timeline();
                $timeL = $timeTables->getTimelineByLecture($item->lecture_id);
                foreach ($timeL as $timeTable){
                   $timeTable->delete();
                }
            }
            if(substr($e->getMessage(),36) == 'Private video'){
                $videos = new Model_Lecture($item->lecture_id);
                $videos->writecode = 'dub';
                $videos->save();
                $ranks = new Model_Rank();
                $ran = $ranks->getRankByLectureId($item->lecture_id);
                foreach ($ran as $rank){
                   $rank->delete();
                }
                $timeTables = new Model_Timeline();
                $timeL = $timeTables->getTimelineByLecture($item->lecture_id);
                foreach ($timeL as $timeTable){
                        $timeTable->delete();
                }
            }
            
        }
        }   
        
        
    }
}